package hanlen.mq.starter.configuration;


import hanlen.mq.starter.RedissonBeanProcessor;
import hanlen.mq.starter.configuration.properties.HanlenMqProperties;
import hanlen.mq.starter.core.RedisQueueExportService;
import hanlen.mq.starter.core.RedisQueueInterceptor;
import hanlen.mq.starter.core.RedissonService;
import hanlen.mq.starter.core.ThreadPoolExecutorService;
import hanlen.mq.starter.support.ExtensionLoader;
import hanlen.mq.starter.support.RedisQueueDlqSupport;
import hanlen.mq.starter.support.thread.AsyncThreadPoolChecker;
import java.util.Map;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * mq-spring-boot-starter
 *
 * @author MrYe
 * @date 2021-10-25 17:30
 */
@EnableConfigurationProperties(HanlenMqProperties.class)
public class MqAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(ExtensionLoader.class)
    public ExtensionLoader extensionLoader() {
        return new ExtensionLoader();
    }

    @Bean
    @ConditionalOnMissingBean(RedissonService.class)
    public RedissonService redissonService(ExtensionLoader extensionLoader) {
        final Map<String, Object> extensionMap = extensionLoader.getExtensionLoader(RedissonService.class);
        return (RedissonService) extensionMap.get("redisson");
    }

    @Bean
    @ConditionalOnMissingBean(ThreadPoolExecutorService.class)
    public ThreadPoolExecutorService threadPoolExecutorService(ExtensionLoader extensionLoader) {
        return (ThreadPoolExecutorService) extensionLoader.getExtensionLoader(ThreadPoolExecutorService.class).get("thread");
    }

    @Bean
    @ConditionalOnMissingBean(RedisQueueInterceptor.class)
    public RedisQueueInterceptor redisQueueInterceptor(ExtensionLoader extensionLoader) {
        return (RedisQueueInterceptor) extensionLoader.getExtensionLoader(RedisQueueInterceptor.class).get("interceptor");
    }

    @Bean
    @ConditionalOnMissingBean(RedisQueueExportService.class)
    public RedisQueueExportService redisQueueExportService(ExtensionLoader extensionLoader) {
        return (RedisQueueExportService) extensionLoader.getExtensionLoader(RedisQueueExportService.class).get("export");
    }

    @Bean
    @ConditionalOnMissingBean(RedisQueueDlqSupport.class)
    public RedisQueueDlqSupport redisQueueDlqSupport() {
        return new RedisQueueDlqSupport();
    }

    @Bean
    @ConditionalOnMissingBean(RedissonBeanProcessor.class)
    public RedissonBeanProcessor redissonBeanProcessor() {
        return new RedissonBeanProcessor();
    }

    @Bean
    @ConditionalOnMissingBean(AsyncThreadPoolChecker.class)
    public AsyncThreadPoolChecker asyncThreadPoolChecker() {
        return new AsyncThreadPoolChecker();
    }

}
